package com.market.manage.controller;

import com.market.manage.mapper.UsersMapper;
import com.market.manage.po.Users;
import com.market.manage.service.MailService;
import com.market.manage.service.UsersService;
import com.market.po.Result;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping(value = "/users")
public class UsersController {
    @Resource
    private MailService mailServiceImpl;

    @Resource
    private UsersService usersServiceImpl;

    @GetMapping("/leave")
    public void leave(String id, String name, String reason, HttpServletRequest request) throws ParseException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        String[] time = parameterMap.get("time[]");
        usersServiceImpl.leave(id, name, time, reason);
    }

    @GetMapping("/piZhun")
    public void piZhun(String id) throws MessagingException {
        usersServiceImpl.piZhun(id);
    }

    @PostMapping("/login")
    public Result login(Users users) {
        return usersServiceImpl.login(users);
    }

    @GetMapping("/check")
    public Long check(Long userId) {
        return usersServiceImpl.check(userId);
    }

    @GetMapping("/getUsers")
    public Result getUsers(String role, String name, Long pageIndex, Long pageSize) {
        return usersServiceImpl.getUsers(role, name, pageIndex, pageSize);
    }

    @GetMapping("/getAllUsers")
    public Result getAllUsers() {
        return usersServiceImpl.getAllUsers();
    }

    @PostMapping("/edit")
    public void edit(String base64, Users user) {
        usersServiceImpl.edit(base64, user);
    }

    @PostMapping("/add")
    public void add(MultipartFile file, Users user, HttpServletRequest request) {
        usersServiceImpl.add(file, user);
        System.out.println(request);
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        return usersServiceImpl.delete(id);
    }

    @GetMapping("/countUsers")
    public Long countUsers() {
        return usersServiceImpl.countUsers();
    }

    @GetMapping("/updPass")
    public int updPass(String username, String password, String verification, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String sessionVerification = (String) session.getAttribute("verificationCode");
        if (StringUtils.isEmpty(sessionVerification)) {
            return 3;
        }
        if (!StringUtils.isEmpty(verification)) {
            if (verification.equals(sessionVerification)) {
                //验证成功 修改密码
                usersServiceImpl.pass(username, password);
            } else {
                //验证码错误
                return 2;
            }
        }
        return 1;
    }

    @GetMapping("/verification")
    public int verification(String username, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String radomString = radomString().toUpperCase();
        session.setAttribute("verificationCode", radomString);
        session.setMaxInactiveInterval(60 * 3);
        Users user = usersServiceImpl.getUser(username);
        if (user != null) {
            try {
                mailServiceImpl.sendHtmlMail(user.getEmail(), "验证码为", radomString);
                //验证码发送成功
                return 1;
            } catch (MessagingException e) {
                //验证码发送失败
                return 3;
            }
        } else {
            //无此用户
            return 2;
        }
    }

    /**
     * 修改密码
     */
    @GetMapping("/pass")
    public void pass(String username, String password) {
        usersServiceImpl.pass(username, password);
    }

    /**
     * 随机生成验证码
     *
     * @return
     */
    private static String radomString() {
        String result = "";
        for (int i = 0; i < 6; i++) {
            int intVal = (int) (Math.random() * 26 + 97);
            result = result + (char) intVal;
        }
        return result;
    }
}
